package com.elinkway.infinitemovies.utils;

import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;

import java.lang.reflect.Field;

/**
 * Created by richard on 2017/7/14.
 */
public class HostUtil {

    private static final String FIELD_PRODUCT_HOST = "PRODUCT_HOST";
    private static final String FIELD_PRODUCT_AD_HOST = "PRODUCT_AD_HOST";
    private static final String FIELD_SEARCH_UPLOAD = "SEARCH_POST_URL_COMMON";
    private static final String FIELD_DATA_REPORT = "DATA_REPORT_URL";

    public static class BaseHost {
        public static String PRODUCT_HOST = "https://www.shandianshipin.cn/";
        public static String PRODUCT_AD_HOST = "http://ad.shandianshipin.cn/";
        public static String SEARCH_POST_URL_COMMON = "http://u-search-dc.shandianshipin.cn/data_collect.so";
        public static String DATA_REPORT_URL = "https://u-data.shandianshipin.cn/api/v1/data/report";
    }

    public static class OnlineHost extends BaseHost {
    }

    public static class TestHost extends BaseHost {
        public static String PRODUCT_HOST = "http://test.api.shandianshipin.cn/";
        public static String PRODUCT_AD_HOST = "http://test.ad.shandianshipin.cn/";
        public static String DATA_REPORT_URL = "http://test.report.shandianshipin.cn/api/v1/data/report";
    }

    public static class UcloudHost extends BaseHost {
        public static String PRODUCT_HOST = "https://u-api.shandianshipin.cn/";
        public static String PRODUCT_AD_HOST = "https://u-ad.shandianshipin.cn/";
        public static String SEARCH_POST_URL_COMMON = "http://u-search-dc.shandianshipin.cn/data_collect.so";
    }

    public static class UcloudTestHost extends BaseHost {
        public static String PRODUCT_HOST = "https://u-api-test.shandianshipin.cn/";
        public static String PRODUCT_AD_HOST = "https://u-ad-test.shandianshipin.cn/";
        public static String SEARCH_POST_URL_COMMON = "http://u-search-dc-test.shandianshipin.cn/data_collect.so";
        public static String DATA_REPORT_URL = "http://test.report.shandianshipin.cn/api/v1/data/report";
    }

    public static class AliHost extends BaseHost {
        public static String PRODUCT_HOST = "https://api.shandianshipin.cn/";
        public static String SEARCH_POST_URL_COMMON = "http://search-dc.shandianshipin.cn/data_collect.so";
    }

    public static Class getClassByHostName(String hostName) {
        if (!TextUtils.isEmpty(hostName)) {
            if (TestHost.PRODUCT_HOST.equals(hostName)) {
                return TestHost.class;
            } else if (UcloudHost.PRODUCT_HOST.equals(hostName)) {
                return UcloudHost.class;
            } else if (UcloudTestHost.PRODUCT_HOST.equals(hostName)) {
                return UcloudTestHost.class;
            } else if (AliHost.PRODUCT_HOST.equals(hostName)) {
                return AliHost.class;
            }
        }
        return OnlineHost.class;
    }

    public static String getFieldValue(Class<?> clazz, String name) {
        String value = null;
        if (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
                field.setAccessible(true);
                value = (String) field.get(clazz);
                LogUtils.e("host env field name:" + name + " " + value);
            } catch (Exception e) {
                LogUtils.e(e.getMessage());
            }
            if (field == null) {
                clazz = clazz.getSuperclass();
                if (Object.class != clazz) {
                    return getFieldValue(clazz, name);
                }
            }
        }
        return value;
    }

    public static String getHost(String hostName) {
        Class clazz = getClassByHostName(hostName);
        LogUtils.e("host env host:" + clazz.getName());
        String result = getFieldValue(clazz, FIELD_PRODUCT_HOST);
        if (TextUtils.isEmpty(result)) {
            result = OnlineHost.PRODUCT_HOST;
        }
        return result;
    }

    public static String getAdHost(String hostName) {
        Class clazz = getClassByHostName(hostName);
        LogUtils.e("host env host:" + clazz.getName());
        String result = getFieldValue(clazz, FIELD_PRODUCT_AD_HOST);
        if (TextUtils.isEmpty(result)) {
            result = OnlineHost.PRODUCT_AD_HOST;
        }
        return result;
    }

    public static String getSearchUploadHost(String hostName) {
        Class clazz = getClassByHostName(hostName);
        LogUtils.e("host env host:" + clazz.getName());
        String result = getFieldValue(clazz, FIELD_SEARCH_UPLOAD);
        if (TextUtils.isEmpty(result)) {
            result = OnlineHost.SEARCH_POST_URL_COMMON;
        }
        return result;
    }

    public static String getDataReportHost(String hostName) {
        Class clazz = getClassByHostName(hostName);
        LogUtils.e("host env host:" + clazz.getName());
        String result = getFieldValue(clazz, FIELD_DATA_REPORT);
        if (TextUtils.isEmpty(result)) {
            result = OnlineHost.DATA_REPORT_URL;
        }
        return result;
    }

}
